package com.example.project.controller;

import com.example.project.service.ExcelService;
import com.example.utils.resultFormat.Result;
import com.example.utils.resultFormat.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api("Excel操作接口")
@RequestMapping("/excelController")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    /**
     * 导出Excel报表模板
     */
    @GetMapping(value = "/exportExcelTemplate")
    @ResponseBody
    public void exportExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        excelService.exportExcelTemplate(request,response);
    }

    /**
     * 导入Excel报表
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result importExcel(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Result result = excelService.readExcelFile(file);
        return result;
    }

    /**
     * 导出Excel报表
     */
    @GetMapping(value = "/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
         excelService.exportExcel(request,response);
    }


    /**
     * 获取Excel报表数据
     */
    @GetMapping(value = "/getExcelInfo")
    @ResponseBody
    public Result getExcelInfo(){
        return ResultUtil.success(excelService.getExcelInfo());
    }
}
